﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NIKE_Project.DAL;

namespace NIKE_Project.BLL
{
    public class CollectMoney
    {

        //调用DBHelper类
        DBHelper db = new DBHelper();

        //定义DataSet全局变量
        public DataSet ds;

      

        public void DeleteRow(DataGridView dataGridView1)
        {
            dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);//删除选中行
        }

        public string AddSalesDetails(DataGridView dataGridView1,string runningWater)
        {
            //添加销售明细记录
            //----//
            //获取销售记录ID
            string ID = string.Format("select SalesID from Sales where ReceiptsCode='{0}'", runningWater);
            ds = db.getDataSet(ID);
            string SalesID = ds.Tables[0].Rows[0]["SalesID"].ToString();
            //遍历购物车 获取每件商品的信息
            string SalesDetail = "";
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                //获取商品ID
                string barCode = dataGridView1.Rows[i].Cells[0].Value.ToString();
                string Goods = string.Format("select GoodsID from Goods where BarCode='{0}'", barCode);
                ds = db.getDataSet(Goods);
                string GoodsID = ds.Tables[0].Rows[0]["GoodsID"].ToString();
                //获取购买的商品数量
                string Quantity = dataGridView1.Rows[i].Cells[7].Value.ToString();
                //获取商品成交价格
                string AloneAmount = dataGridView1.Rows[i].Cells[5].Value.ToString();
                //编写SQL语句向销售明细表添加数据  根据购买数量更新相应库存数量
                SalesDetail = AddData(SalesID, GoodsID, Quantity, AloneAmount);
            }
            return SalesDetail;
        }

        public string AddData(string SalesID, string GoodsID, string Quantity, string AloneAmount)
        {
            return string.Format(@"insert into SalesDetail(SalesID,GoodsID,Quantity,AloneAmount) values
					                                      ('{0}','{1}','{2}','{3}') update Goods set StockNum=StockNum-{2} where GoodsID={1}",
                                          SalesID, GoodsID, Quantity, AloneAmount);
        }

        public string SelectGoods(string Bc)
        {
            string Sqlstr = "";
            Sqlstr =  string.Format(@"select GoodsID,BarCode,GoodsName,t.TypeName,SalePrice,Discount 
                       from Goods g,Type t where g.TypeID=t.TypeID and BarCode='{0}'", Bc);
            return Sqlstr;
        }
    }
}
